package com.flatlyapps.shoppinglist;

import com.flatlyapps.shoppinglist.R;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;

public class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener{

	public static final String TITLE_KEY = "title_pref";
	public static final String BUDGET_KEY = "budget_pref";
	public static final String CURRENCY_KEY = "currency_pref";
	public static final String COLOR_KEY = "color_pref";

	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);

		EditTextPreference budget = (EditTextPreference) findPreference(BUDGET_KEY);
		budget.getEditText().addTextChangedListener(new DecimalPlaceListener(2));
	}

	@Override
	public void onResume() {
		super.onResume();
		SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
		sharedPreferences.registerOnSharedPreferenceChangeListener(this);

		Preference titlePref = findPreference(TITLE_KEY);
		titlePref.setSummary("The default list title is " + sharedPreferences.getString(TITLE_KEY, ""));

		Preference budgetPref = findPreference(BUDGET_KEY);
		budgetPref.setSummary("The default budget is " + sharedPreferences.getString(BUDGET_KEY, ""));

		Preference currencyPref = findPreference(CURRENCY_KEY);
		currencyPref.setSummary("The default currency is " + sharedPreferences.getString(CURRENCY_KEY, ""));

		Preference colorPref = findPreference(COLOR_KEY);
		String colorIndexString = sharedPreferences.getString(SettingsFragment.COLOR_KEY, "3");
		int colorIndex = Integer.valueOf(colorIndexString);
		String color = getResources().getStringArray(R.array.color_pref_entries)[colorIndex];
		colorPref.setSummary("The default colour is " + color);
	}

	@Override
	public void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	}

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		if (key.equals(TITLE_KEY)) {
			Preference titlePref = findPreference(key);
			// Set summary to be the user-description for the selected value
			titlePref.setSummary("The default list title is " + sharedPreferences.getString(key, ""));
		}
		if (key.equals(BUDGET_KEY)) {
			Preference budgetPref = findPreference(key);
			// Set summary to be the user-description for the selected value
			budgetPref.setSummary("The default budget is " + sharedPreferences.getString(key, ""));
		}
		if (key.equals(CURRENCY_KEY)) {
			Preference currencyPref = findPreference(key);
			// Set summary to be the user-description for the selected value
			currencyPref.setSummary("The default currency is " + sharedPreferences.getString(key, ""));
		}
		if (key.equals(COLOR_KEY)) {
			Preference colorPref = findPreference(key);
			// Set summary to be the user-description for the selected value
			String colorIndexString = sharedPreferences.getString(SettingsFragment.COLOR_KEY, "3");
			int colorIndex = Integer.valueOf(colorIndexString);
			String color = getResources().getStringArray(R.array.color_pref_entries)[colorIndex];
			colorPref.setSummary("The default colour is " + color);
		}
	}

}
